﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Threading;

namespace 高级控件演示.ViewModels
{
    public class SpeekContentViewModel:BindableBase
    {
        private SpeechSynthesizer synthesizer = new SpeechSynthesizer();

        private FlowDocument _document;
        public FlowDocument Document
        {
            get => _document;
            set { _document = value; RaisePropertyChanged(); }
        }


        private string speekContent;


        public string SpeekContent
        {
            get { return speekContent; }
            set
            {
                speekContent = value;
                RaisePropertyChanged();
            }
        }

        public DelegateCommand SpeekCommand { get; private set; }

        public SpeekContentViewModel()
        {
            SpeekContent = "坦克,颜色:雨点,雄鸽,体重:2.2斤\r\n" +
                           "芷儿凤凰鸽舍镇棚鸽";

            SpeekCommand = new DelegateCommand(SpeekCommandFunc);


            Document = new FlowDocument();
            Document.FontFamily = new FontFamily("华文行楷");
            Document.FontSize = 38;  // 单位是像素
            Document.LineHeight = 1.5;  // 可选设置行距
            Document.Blocks.Clear();
            Document.Blocks.Add(new Paragraph(new Run(SpeekContent)));
        }

        private void SpeekCommandFunc()
        {
            //synthesizer.SpeakProgress += OnSpeakProgress;
            Task.Run(async () =>
            {
                int totalLength = Document.ContentStart.GetOffsetToPosition(Document.ContentEnd);

                for (int i = 0; i <= totalLength; i++)
                {
                    HighlightText(0, i, Brushes.Red);
                    await Task.Delay(260);
                }
                HighlightText(0, totalLength, Brushes.Black,38);
            });

            synthesizer.SpeakAsync(SpeekContent);


        }

        private async void OnSpeakProgress(object sender, SpeakProgressEventArgs e)
        {
            //int length = SpeekContent.Length;


            // UTF-8编码计算
            //int utf8Len = Encoding.UTF8.GetByteCount(SpeekContent); // 包含换行符和制表符


        }


        public void HighlightText(int startPos, int length, Brush brush,double fontSize=38.0)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                TextPointer startPointer = Document.ContentStart.GetPositionAtOffset(startPos);
                TextPointer endPointer = startPointer?.GetPositionAtOffset(startPos + length);
                if (startPointer == null || endPointer == null)
                {
                    // 处理越界情况
                    endPointer = Document.ContentEnd;
                }

                var range = new TextRange(
                    startPointer,
                    endPointer
                );
                range.ApplyPropertyValue(TextElement.FontSizeProperty, fontSize);
                range.ApplyPropertyValue(TextElement.ForegroundProperty, brush);
            });


        }
    }
}
